﻿using SpaceNeedle.City6473.Web.Data.Models;
using SpaceNeedle.SDK.Web.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using SpaceNeedle.City6473.Web.Data.Helpers;

namespace SpaceNeedle.City6473.Web.Data.Repositories
{
    public class RequestItemRepository : EntitiyFrameworkRepository<RequestItem>
    {
        public const string ContainerName = "requests";

        public RequestItemRepository() : base(new City6473Entities())
        {
            this.dataContext.Configuration.ProxyCreationEnabled = false;
        }

        public IEnumerable<RequestItem>GetAllByDateDescending()
        {
            return this.GetAll().OrderByDescending(x => x.CreationDate);
        }

        public IEnumerable<RequestItem>GetAllByDateDescendingWithUser()
        {
            var items = this.GetAll().Include(x=>x.User).OrderByDescending(x => x.CreationDate).ToList();

            List<RequestItem> newItems = new List<RequestItem>();

            foreach (var item in items)
            {
                newItems.Add(item.Clone());
            }

            return newItems; 
        }

        public IEnumerable<RequestItem> GetAllByDateDescendingWithUserByUser(Guid userId)
        {
            var items = this.GetAll().Where(x=>x.CreatedBy == userId).Include(x => x.User).OrderByDescending(x => x.CreationDate).ToList();

            List<RequestItem> newItems = new List<RequestItem>();

            foreach (var item in items)
            {
                newItems.Add(item.Clone());
            }

            return newItems;
        }


        public override RequestItem GetById(Guid id)
        {
            return this.GetAll().Where(x => x.Id == id).Include(x=>x.Request).SingleOrDefault();
        }

        public override RequestItem GetById(string id)
        {
            if(string.IsNullOrEmpty(id))
            {
                return null;
            }

            return this.GetById(new Guid(id));
        }

        
        public string GetNextProtocolNumber()
        {
            //2014 08 SN 9999999999
            const int minimunProcolNumber = 999;
            const int maximunProcolNumber = 9999999;
            string symbol = "SN";

            //long totalItems = this.GetAll().Count();

            //if(totalItems < minimunProcolNumber)
            //{
            //    totalItems = minimunProcolNumber;
            //}

            string newNumber = string.Empty;

            RequestItem item = null;
            Random rnd = new Random();
            do
            {
                newNumber = string.Format("{0}{1}{2}", DateTime.Now.ToString("yyyyMM"), symbol, rnd.Next(minimunProcolNumber,  maximunProcolNumber));
                item = this.GetAll().Where(x => x.Number == newNumber).SingleOrDefault();
    
            } while (item != null);

            return newNumber;
        }
    }
}
